package ${package.Controller}

<#if superControllerClassPackage??>
import ${superControllerClassPackage}
</#if>
import com.common.bean.Pager
import com.common.bean.Result
import com.common.enums.Status
import com.common.enums.YesNo
import com.gsdk.security.entity.${entity}
import com.gsdk.security.service.${table.serviceName}
import org.slf4j.Logger
import org.slf4j.LoggerFactory
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController
<#else>
import org.springframework.stereotype.Controller
</#if>
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import javax.annotation.Resource
import javax.validation.Valid

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${table.controllerPath}<#else>${table.entityPath}</#if>")
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}() {</#if>

    private val logger: Logger = LoggerFactory.getLogger(${table.controllerName}::class.java)

    @Resource
    private lateinit var ${serviceNameSimple}Service: ${table.serviceName}

    @RequestMapping
    fun page(map: ModelMap): String {
        map["status"] = Status.common
        map["yesNo"] = YesNo.common
        return "${controllerMappingHyphenSimple}/list"
    }

    @RequestMapping("/list")
    @ResponseBody
    fun list(pager: Pager<${entity}>, value: ${entity}): Any {
        return ${serviceNameSimple}Service.getList(pager, value)
    }

    @RequestMapping("/edit")
    fun edit(map: ModelMap, idx: Long?): String {
        map["status"] = Status.common
        map["yesNo"] = YesNo.common

        if (idx != null) {
            val ${serviceNameSimple} = ${serviceNameSimple}Service.detail(idx)
            map["${serviceNameSimple}"] = ${serviceNameSimple}

            <#list table.fields as field>
            <#if field.propertyName == "locationId">
            initLocation(map, ${serviceNameSimple}!!.locationId!!)
            </#if>
            </#list>
        }

        return "${controllerMappingHyphenSimple}/edit"
    }

    @RequestMapping("/save")
    @ResponseBody
    fun save(@Valid param: ${entity}): Result<Void> {
        ${serviceNameSimple}Service.saveOrUpdate(param)
        return Result.SUCCESS()
    }

    @RequestMapping("/detail")
    fun detailPage(map: ModelMap, idx: Long): String {
        map["${serviceNameSimple}"] = ${serviceNameSimple}Service.detail(idx)
        return "${controllerMappingHyphenSimple}/detail"
    }

    @RequestMapping("/{idx}")
    @ResponseBody
    fun detail(@PathVariable("idx") idx: Long): Result<${entity}> {
        val value = ${serviceNameSimple}Service.detail(idx) ?: return Result.ERROR("${table.comment!}不存在或无权访问")
        return Result.SUCCESS(value)
    }

    @RequestMapping("/delete")
    @ResponseBody
    fun delete(idx: Long): Result<Void> {
        ${serviceNameSimple}Service.removeById(idx)
        return Result.SUCCESS()
    }

}
